package com.HospitalOutpatient.service.impl;

import com.HospitalOutpatient.dao.DepartmentDao;
import com.HospitalOutpatient.dao.OutpatientDao;
import com.HospitalOutpatient.entity.Department;
import com.HospitalOutpatient.entity.Outpatient;
import com.HospitalOutpatient.service.OutpatientService;
import com.HospitalOutpatient.vo.OutpatientAndDepartName;
import com.HospitalOutpatient.vo.OutpatientValue;
import com.HospitalOutpatient.vo.OutpatientVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("outpatientService")
public class OutpatientImpl implements OutpatientService {

    @Autowired
    private OutpatientDao outpatientDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<OutpatientValue> getAllOutpatient() {
        List<Outpatient> outpatientList = outpatientDao.getAllOutpatient();
        List<OutpatientValue> outpatientValueList = new ArrayList<>();

        for (Outpatient outpatient : outpatientList) {
            OutpatientValue outpatientValue = new OutpatientValue();
            outpatientValue.setValue(outpatient.getOutpatientName());
            outpatientValueList.add(outpatientValue);
        }
        return outpatientValueList;
    }

    @Override
    public List<OutpatientVo> getAllOutpatientByDepartmentId() {

        List<Department> departmentList = departmentDao.selectAllDepartment();

        List<OutpatientVo> outpatientVoList = new ArrayList<>();

        for (Department department : departmentList) {
            OutpatientVo fatherVo = new OutpatientVo();

            fatherVo.setValue(department.getDepartmentId());
            fatherVo.setLabel(department.getDepartmentName());

            List<Outpatient> outpatientList = outpatientDao.selectOutpatientByDepartmentId(department.getDepartmentId());

            List<OutpatientVo> outpatientVos = new ArrayList<>();

            for (Outpatient outpatient : outpatientList) {
                OutpatientVo childVo = new OutpatientVo();
                 childVo.setValue(outpatient.getOutpatientId());
                 childVo.setLabel(outpatient.getOutpatientName());
                 outpatientVos.add(childVo);
            }
            fatherVo.setChildren(outpatientVos);
            outpatientVoList.add(fatherVo);
        }

        return outpatientVoList;
    }

    @Override
    public List<OutpatientAndDepartName> getAllOutpatientAndDepartName() {
        List<Outpatient> outpatientList = outpatientDao.getAllOutpatient();
        List<OutpatientAndDepartName> outpatientAndDepartNameList = new ArrayList<>();
        for (Outpatient outpatient : outpatientList) {
            Department department = departmentDao.getDepartmentById(outpatient.getDepartmentId());

            OutpatientAndDepartName outpatientAndDepartName = new OutpatientAndDepartName();

            outpatientAndDepartName.setOutpatientId(outpatient.getOutpatientId());
            outpatientAndDepartName.setOutpatientName(outpatient.getOutpatientName());
            outpatientAndDepartName.setDepartmentName(department.getDepartmentName());

            outpatientAndDepartNameList.add(outpatientAndDepartName);
        }
        return outpatientAndDepartNameList;
    }

    @Override
    public boolean addOutpatient(Outpatient outpatient) {

        int i = outpatientDao.insertOutpatient(outpatient);
        return i == 1;
    }

    @Override
    public boolean editOutpatient(Outpatient outpatient) {
        int i = outpatientDao.updateOutpatient(outpatient);
        return i == 1;
    }
}
